package or.exa.cangguan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import or.exa.cangguan.common.Page;
import or.exa.cangguan.entity.dto.GoodsDto;
import or.exa.cangguan.entity.po.Goods;
import or.exa.cangguan.entity.po.OutOperation;
import or.exa.cangguan.entity.po.OutStore;
import or.exa.cangguan.entity.po.OutStoreDetail;
import or.exa.cangguan.entity.vo.GoodsVo;
import or.exa.cangguan.entity.vo.OutStoreVo;
import or.exa.cangguan.mapper.*;
import or.exa.cangguan.service.OutStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class OutStoreServiceImpl implements OutStoreService {

    @Autowired
    private OutStoreMapper outStoreMapper;
    @Autowired
    private OutStoreDetailMapper outStoreDetailMapper;

    @Autowired
    private OutOperationMapper outOperationMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addOutStore(OutStoreVo outStoreVo) {
        Date date = new Date(System.currentTimeMillis());
        log.info("outStoreVo:{}", outStoreVo);

        OutStore outStore = new OutStore();
        BeanUtil.copyProperties(outStoreVo, outStore);
        outStore.setCreateTime(date);
        outStore.setCreateBy(0);
        outStore.setUpdateBy(0);
        outStore.setUpdateTime(date);
        outStore.setOutStatus("0"); // 设置初始状态为未出库

        // 插入出库单主表
        Integer storeId = storeMapper.selectByName(outStoreVo.getStoreName());
        outStore.setStoreId(storeId);
        int i = outStoreMapper.insertSelective(outStore);
        log.info("outStore:{}", outStore);

        //新增作业单
        OutOperation outOperation = new OutOperation();
        outOperation.setOutId(outStore.getId());
        outOperation.setStatus("0");
        outOperation.setCreateTime(date);
        outOperation.setCreateBy(0);
        outOperation.setUpdateBy(0);
        outOperation.setUpdateTime(date);

        outOperationMapper.insertSelective(outOperation);

        // 获取商品信息
        List<Goods> goodsList = goodsMapper.selectByName(outStoreVo.getGoodsList());

        // 插入出库单明细
        int detailCount = 0;
        for (GoodsVo goodsVo : outStoreVo.getGoodsList()) {
            OutStoreDetail outStoreDetail = new OutStoreDetail();
            outStoreDetail.setOutId(outStore.getId());
            // 根据商品名称找到对应的商品ID
            for (Goods goods : goodsList) {
                if (goods.getGoodsName().equals(goodsVo.getGoodsName())) {
                    outStoreDetail.setGoodsId(goods.getGoodsId());
                    break;
                }
            }
            outStoreDetail.setGoodsCount(goodsVo.getGoodsCount());
            outStoreDetail.setCreateTime(date);
            outStoreDetail.setUpdateTime(date);
            outStoreDetail.setCreateBy(0);
            outStoreDetail.setUpdateBy(0);

            detailCount += outStoreDetailMapper.insertSelective(outStoreDetail);
        }

        return i > 0 && detailCount == outStoreVo.getGoodsList().size() ? 1 : 0;
    }

    @Override
    public int updateOutStore(OutStoreVo outStoreVo) {
        Date date = new Date(System.currentTimeMillis());
        OutStore outStore = new OutStore();
        BeanUtil.copyProperties(outStoreVo, outStore);
        outStore.setUpdateBy(0);
        outStore.setUpdateTime(date);


        Integer storeId = null;
        if (outStoreVo.getStoreName() != null && !outStoreVo.getStoreName().isEmpty()) {
            storeId = storeMapper.selectByName(outStoreVo.getStoreName());
        }
        outStore.setStoreId(storeId);

        int i = outStoreMapper.updateByPrimaryKeySelective(outStore);

        List<Goods> goodsList = goodsMapper.selectByName(outStoreVo.getGoodsList());
        log.info("outStore.getId():" + outStore.getId());
        outStoreDetailMapper.deleteByPrimaryKey(outStore.getId());
        int detailCount = 0;
        for (GoodsVo goodsVo : outStoreVo.getGoodsList()) {
            OutStoreDetail outStoreDetail = new OutStoreDetail();
            outStoreDetail.setOutId(outStore.getId());
            // 根据商品名称找到对应的商品ID
            for (Goods goods : goodsList) {
                if (goods.getGoodsName().equals(goodsVo.getGoodsName())) {
                    outStoreDetail.setGoodsId(goods.getGoodsId());
                    break;
                }
            }
            outStoreDetail.setGoodsCount(goodsVo.getGoodsCount());
            outStoreDetail.setCreateTime(date);
            outStoreDetail.setUpdateTime(date);
            outStoreDetail.setCreateBy(0);
            outStoreDetail.setUpdateBy(0);

            detailCount += outStoreDetailMapper.insertSelective(outStoreDetail);
        }

        return i > 0 && detailCount == outStoreVo.getGoodsList().size() ? 1 : 0;
    }

    @Override
    public Page searchOutStore(OutStoreVo outStoreVo) {
        Page page = new Page();

        BeanUtil.copyProperties(outStoreVo, page);
        Integer storeId = null;
        if (outStoreVo.getStoreName() != null && outStoreVo.getStoreName()!="") {
            storeId = storeMapper.selectByName(outStoreVo.getStoreName());
        }
        page.setResultList(outStoreMapper.selectOutStore(storeId, outStoreVo.getOutNumber(), outStoreVo.getOutPlanTime(),page));

        Integer totalNum = outStoreMapper.selectOutStoreCount(storeId, outStoreVo.getOutNumber(), outStoreVo.getOutPlanTime());
        page.setTotalNum(totalNum);

        return page;
    }

    @Override
    public int deleteOutStore(Integer id) {
        int i = outStoreMapper.deleteByPrimaryKey(id);
        int j = outStoreDetailMapper.deleteByPrimaryKey(id);

        return i + j;
    }

    @Override
    public List<GoodsDto> searchOutStoreGoods(Integer id) {
        return outStoreDetailMapper.selectByOutId(id);
    }

    @Override
    public int updateStatus(Integer id, String status, Date outPlanTime) {
        if (status.equals("2")) {
            outOperationMapper.deleteByOutId(id);
        }
        return outStoreMapper.updateOutStatus(id, status, outPlanTime);
    }
}
